import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Solution785 {
    public boolean isBipartite(int[][] graph) {
        int[] visit=new int[graph.length];
        Arrays.fill(visit,-1);
        Queue<Integer> queue=new LinkedList<>();
        int t;
        while ((t=check(visit))!=-1){
            visit[t]=0;
            queue.offer(t);
            while (!queue.isEmpty()){
                int m=queue.poll();
                for (int i=0;i<graph[m].length;i++){
                    if (visit[m]==visit[graph[m][i]]){
                        return false;
                    }
                    if (visit[graph[m][i]]==-1){
                        visit[graph[m][i]]=1-visit[m];
                        queue.offer(graph[m][i]);
                    }
                }
            }}

        return true;
    }

    private int check(int[] visit) {
        for (int i=0;i<visit.length;i++){
            if (visit[i]==-1){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution785().isBipartite(new int[][]{{1,2,3},{0,2},{0,1,3},{0,2}}));
    }
}
